package com.tt.controller;
import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.tt.entity.Hotel;
import com.tt.service.HotelserviceImpl;

/**
 * @author hqr
 * 2019.12.24
 */

@Controller
public class HotelController {
	 @Resource
	private HotelserviceImpl hotelserviceImpl;
	 
	 
	 
	 //��ѯ����
	 @RequestMapping("hotelfoundAll")
	 public String hotelfoundAll(Model model){
	List<Hotel>	hotellist= hotelserviceImpl.hotelfoundAll();
		 model.addAttribute("hotellists",hotellist);
		 return "hotelindex.jsp";
	 }
     
	 
	 //���
	 @RequestMapping(value="addHotel",method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
   public String addHotel(Hotel hotel,@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws IllegalStateException, IOException{
		 System.out.println(hotel);
		  if(!file1.isEmpty()){			  
			  file1.transferTo(new File(request.getServletContext().getRealPath("img"),file1.getOriginalFilename()));
			  hotel.setImg(file1.getOriginalFilename());
		  }  
	boolean result = hotelserviceImpl.addHotel(hotel);
	   return "hotelfoundAll";
	 }
	 
	 //ɾ��
	 @RequestMapping("deleteHotel")
	  public String deleteHotel(String id){
		 int ids=Integer.parseInt(id);
		 hotelserviceImpl.deteleHotel(ids);
		 return "hotelfoundAll";
	  }
	 
	 //�޸�
	 @RequestMapping(value="updateHotel",method = RequestMethod.POST,produces = "text/plain;charset=UTF-8")
	 public String updateHotel(Hotel hotel,@RequestParam("file1") MultipartFile file1,HttpServletRequest request)throws IllegalStateException, IOException{
		 if(!file1.isEmpty()){			  
			  file1.transferTo(new File(request.getServletContext().getRealPath("img"),file1.getOriginalFilename()));
			  hotel.setImg(file1.getOriginalFilename());
		 }
		 hotelserviceImpl.updateHotel(hotel);
		 return "hotelfoundAll";
	 }
	   //id ����
	  @RequestMapping("showHotel")
		 public String showHotel(String id,Model model){
		  int ids=Integer.parseInt(id);
		  Hotel hotel=hotelserviceImpl.showHotel(ids);
			 model.addAttribute("hotel",hotel);
			 return "updatehotel.jsp";
	}
	  @RequestMapping("showHotelsingle")
	  public String showHotelsingle(String id,Model model){
		  int ids=Integer.parseInt(id);
		  Hotel hotel=hotelserviceImpl.showHotel(ids);
			 model.addAttribute("hotel",hotel);
			 return "hotelsingle.jsp";
}
	  }
